package com.flowable.demo;

import java.util.List;

import org.flowable.bpmn.model.Activity;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.FlowableEngineAgenda;
import org.flowable.engine.impl.bpmn.behavior.MultiInstanceActivityBehavior;
import org.flowable.engine.impl.history.HistoryManager;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager;
import org.flowable.engine.impl.util.ProcessDefinitionUtil;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntityManager;

public class CustomCommonJumpCmd implements Command<Void> {

	private String taskId = "";
	private String targetNodeId = "";

	public CustomCommonJumpCmd(String taskId, String targetNodeId) {
		this.taskId = taskId;
		this.targetNodeId = targetNodeId;
	}

	public Void execute(CommandContext commandContext) {
		TaskEntityManager taskEntityManager = org.flowable.task.service.impl.util.CommandContextUtil
				.getTaskEntityManager(commandContext);
		ExecutionEntityManager executionEntityManager = org.flowable.engine.impl.util.CommandContextUtil
				.getExecutionEntityManager(commandContext);

		// 查询任务
		TaskEntity taskEntity = taskEntityManager.findById(taskId);

		String executionId = taskEntity.getExecutionId();
		String processDefinitionId = taskEntity.getProcessDefinitionId();

		// 查询执行实例
		ExecutionEntity executionEntity = executionEntityManager.findById(executionId);
		// 父执行实例
		ExecutionEntity parentExecutionEntity = executionEntity.getParent();
		// 查询流程
		org.flowable.bpmn.model.Process process = ProcessDefinitionUtil.getProcess(processDefinitionId);

		// 当前任务的key
		String currentTaskDefinitionKey = taskEntity.getTaskDefinitionKey();
		// 当前任务
		Activity currentActivity = (Activity) process.getFlowElement(currentTaskDefinitionKey);
		Object currentBehavior = currentActivity.getBehavior();

		// 历史任务
		HistoryManager historyManager = org.flowable.engine.impl.util.CommandContextUtil.getHistoryManager();
		if (currentBehavior instanceof MultiInstanceActivityBehavior) {
			executionEntityManager.deleteChildExecutions(parentExecutionEntity, "deleteReason:delete multiinstance",
					false);
			// 修改is_mi_root_字段
			parentExecutionEntity.setMultiInstanceRoot(false);
			// 修改is_active_
			parentExecutionEntity.setActive(true);
			// 需要更新数据
			executionEntityManager.update(parentExecutionEntity);

		} else {
			// 删除跳转前的任务节点
			taskEntityManager.delete(taskId);
			historyManager.recordTaskEnd(taskEntity, executionEntity, "delete resaon:jump");
			historyManager.recordActivityEnd(parentExecutionEntity, "delete resaon:jump");
		}

		/**
		 * --------------------------------------------------------------执行节点跳转--------------------------------------------------------------
		 */
		FlowElement targetFlowElement = process.getFlowElement(targetNodeId);
		// 指定当前节点为目标节点
		executionEntity.setCurrentFlowElement(targetFlowElement);

		FlowableEngineAgenda flowableEngineAgenda = org.flowable.engine.impl.util.CommandContextUtil.getAgenda();

		if (!(currentBehavior instanceof MultiInstanceActivityBehavior)) {
			
			flowableEngineAgenda.planContinueProcessInCompensation(executionEntity);
			
		} else {
			// 触发一下，让执行实例往下运行
			ExecutionEntity rootExecutionEntity = parentExecutionEntity.getParent();
			List<ExecutionEntity> executionEntities = executionEntityManager
					.findChildExecutionsByParentExecutionId(rootExecutionEntity.getId());
			if (executionEntities.size() > 0) {
				ExecutionEntity oneExecutionEntity = executionEntities.get(0);
				oneExecutionEntity.setCurrentFlowElement(targetFlowElement);
				// 让流程实例运转
				flowableEngineAgenda.planContinueProcessInCompensation(oneExecutionEntity);
			}
		}

		return null;
	}

}
